﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Drawing.Drawing2D;

namespace nsWeldController
{
    public partial class DockSetParameter : DockContent
    {
        private CLog mLog = CWeldController.mLog;
        public BindingList<ActionParas> mAutoActionList = new BindingList<ActionParas>();
        public int originXSet;
        public int originYSet;
        public int originZSet;

        public int polarityCoorX;
        public int polarityCoorY;
        public int polarityCoorZ;

        public uint pwmFrequencyDefault;
        public uint pwmFrequencyReset;
        public uint pwmFrequencyChabu;
        public uint pwmFrequencyGum;
        public uint pwmFrequencyLineMotor;
        public uint pwmChabuDelay;
        public uint gumDelay;

        public DockSetParameter()
        {
            InitializeComponent();
            originXSet = 0;
            originYSet = 0;
            originZSet = 0;

            polarityCoorX = 0;
            polarityCoorY = 0;
            polarityCoorZ = 0;

            pwmFrequencyDefault = 2000;
            pwmFrequencyReset = 100;    // 设置为默认100，TODO:HJW 后续将该按钮设置疲敝 
            pwmFrequencyChabu = 2000;
            pwmFrequencyGum = 150;
            pwmFrequencyLineMotor = 4000;
            pwmChabuDelay = 1000;
            gumDelay = 500;
        }

        /// <summary>
        /// 重写以float方式显示的dock窗体的show方法
        /// 区分是否第一次显示，DefaultFloatWindowSize只在第一次显示的时候有效，窗口hide后再显示时候不再指定显示类型
        /// 这样多个float的大小设置不会相互影响。
        /// </summary>
        private bool isFirstShow = true;
        new public void Show(DockPanel dockPanel)
        {
            if (isFirstShow)
            {
                dockPanel.DefaultFloatWindowSize = new Size(this.Size.Width, this.Size.Height);
                base.Show(dockPanel, DockState.Float);
                isFirstShow = false;
            }
            else
            {
                base.Show(dockPanel);
            }
        }

        private void DockActionRecord_Load(object sender, EventArgs e)
        {
            comboBoxMode.SelectedIndex = 0;
            mAutoActionList.AllowNew = true;
            mAutoActionList.AllowRemove = true;
            mAutoActionList.AllowEdit = true;
            dgvAutoGumList.AutoGenerateColumns = true;
            dgvAutoGumList.AllowUserToAddRows = false;
            dgvAutoGumList.DataSource = mAutoActionList;
            ShowParameter();

            panel1.Show();
            panel2.Hide();
            panel3.Hide();
            panel4.Hide();
            panel2.Location = panel1.Location;
            panel3.Location = panel1.Location;
            panel4.Location = panel1.Location;

            ShowActionTreeView();
        }

        internal void ShowParameter()
        {
            txtOriginXSet.Text = originXSet.ToString();
            txtOriginYSet.Text = originYSet.ToString();
            txtOriginZSet.Text = originZSet.ToString();
            txtPwmFrequencyDefault.Text = pwmFrequencyDefault.ToString();
            txtPwmFrequencyReset.Text = pwmFrequencyReset.ToString();
            txtPwmFrequencyChabu.Text = pwmFrequencyChabu.ToString();
            txtPwmFrequencyGum.Text = pwmFrequencyGum.ToString();
            txtPwmFrequencyLineMotor.Text = pwmFrequencyLineMotor.ToString();
            txtPolarityCoorX.Text = polarityCoorX.ToString();
            txtPolarityCoorY.Text = polarityCoorY.ToString();
            txtPolarityCoorZ.Text = polarityCoorZ.ToString();
            txtChabuDelay.Text = pwmChabuDelay.ToString();
            txtGumDelay.Text = gumDelay.ToString();
        }
        
        private void btnClearAllAction_Click(object sender, EventArgs e)
        {
            mAutoActionList.Clear();
        }

        private void btnRemoveActionSelected_Click(object sender, EventArgs e)
        {
            if (dgvAutoGumList.SelectedRows.Count == 0)
            {
                if (dgvAutoGumList.SelectedCells.Count != 0)
                {
                    // 只有部分单元格被选中的情况下删除最近一次选中单元格所在行
                    dgvAutoGumList.Rows.Remove(dgvAutoGumList.SelectedCells[0].OwningRow);
                }
            }
            else
            {
                // 有整行被选中的情况下删除所有被选中行
                foreach (DataGridViewRow row in dgvAutoGumList.SelectedRows)
                {
                    dgvAutoGumList.Rows.Remove(row);
                }
            }
        }

        /// <summary>
        /// 在DataGridView中增加行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvAutoGumList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, new SolidBrush(Color.CadetBlue), e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加行号时发生错误，错误信息：" + ex.Message, "操作失败");
            }
        }

        private void dgvAutoGumList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            CWeldController.mDockMain.DrawAction();
        }

        private void dgvAutoGumList_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            CWeldController.mDockMain.DrawAction();
        }

        /// <summary>
        /// 将参数信息设置到控制板
        /// </summary>
        public bool SendAllPararmeter()
        {
            int count = mAutoActionList.Count;
            ET_GumAction action = new ET_GumAction();
            int structLen = System.Runtime.InteropServices.Marshal.SizeOf(typeof(ET_GumAction));
            byte[] bytes = new byte[structLen * count];
            for (int i = 0; i < count; i++)
            {
                action.cmd = (int)mAutoActionList[i].Cmd;
                action.tCoord1.x = mAutoActionList[i].Point1X;
                action.tCoord1.y = mAutoActionList[i].Point1Y;
                action.tCoord1.z = mAutoActionList[i].Point1Z;
                action.tCoord2.x = mAutoActionList[i].Point2X;
                action.tCoord2.y = mAutoActionList[i].Point2Y;
                action.tCoord2.z = mAutoActionList[i].Point2Z;
                action.r = mAutoActionList[i].Radius;
                action.mode = mAutoActionList[i].Mode;
                Buffer.BlockCopy(CWeldController.StructToBytes(action), 0, bytes, i * structLen, structLen);
            }
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_SET_PARAMETER;
            stSend.sendMsg = new byte[bytes.Length + 52];
            int n = 0;
            stSend.sendMsg[0 + n * 4] = (byte)(originXSet & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(originXSet >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(originXSet >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(originXSet >> 24 & 0xff);
            n = 1;
            stSend.sendMsg[0 + n * 4] = (byte)(originYSet & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(originYSet >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(originYSet >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(originYSet >> 24 & 0xff);
            n = 2;
            stSend.sendMsg[0 + n * 4] = (byte)(originZSet & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(originZSet >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(originZSet >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(originZSet >> 24 & 0xff);
            n = 3;
            stSend.sendMsg[0 + n * 4] = (byte)(pwmFrequencyDefault & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(pwmFrequencyDefault >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(pwmFrequencyDefault >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(pwmFrequencyDefault >> 24 & 0xff);
            n = 4;
            stSend.sendMsg[0 + n * 4] = (byte)(pwmFrequencyReset & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(pwmFrequencyReset >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(pwmFrequencyReset >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(pwmFrequencyReset >> 24 & 0xff);
            n = 5;
            stSend.sendMsg[0 + n * 4] = (byte)(pwmFrequencyChabu & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(pwmFrequencyChabu >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(pwmFrequencyChabu >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(pwmFrequencyChabu >> 24 & 0xff);
            n = 6;
            stSend.sendMsg[0 + n * 4] = (byte)(pwmFrequencyGum & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(pwmFrequencyGum >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(pwmFrequencyGum >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(pwmFrequencyGum >> 24 & 0xff);
            n = 7;
            stSend.sendMsg[0 + n * 4] = (byte)(pwmFrequencyLineMotor & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(pwmFrequencyLineMotor >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(pwmFrequencyLineMotor >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(pwmFrequencyLineMotor >> 24 & 0xff);
            n = 8;
            stSend.sendMsg[0 + n * 4] = (byte)(polarityCoorX & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(polarityCoorX >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(polarityCoorX >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(polarityCoorX >> 24 & 0xff);
            n = 9;
            stSend.sendMsg[0 + n * 4] = (byte)(polarityCoorY & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(polarityCoorY >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(polarityCoorY >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(polarityCoorY >> 24 & 0xff);
            n = 10;
            stSend.sendMsg[0 + n * 4] = (byte)(polarityCoorZ & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(polarityCoorZ >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(polarityCoorZ >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(polarityCoorZ >> 24 & 0xff);
            n = 11;
            stSend.sendMsg[0 + n * 4] = (byte)(pwmChabuDelay & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(pwmChabuDelay >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(pwmChabuDelay >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(pwmChabuDelay >> 24 & 0xff);
            n = 12;
            stSend.sendMsg[0 + n * 4] = (byte)(count & 0xff);
            stSend.sendMsg[1 + n * 4] = (byte)(count >> 8 & 0xff);
            stSend.sendMsg[2 + n * 4] = (byte)(count >> 16 & 0xff);
            stSend.sendMsg[3 + n * 4] = (byte)(count >> 24 & 0xff);
            Buffer.BlockCopy(bytes, 0, stSend.sendMsg, 52, bytes.Length);
            return EnQueueSend(stSend);
        }


        private bool EnQueueSend(ET_GPES_SENDMSG stSend)
        {
            return CWeldController.mDockSerialProtocol.EnQueueSend(stSend);
        }

        private void txtOriginXSet_TextChanged(object sender, EventArgs e)
        {
            int.TryParse(txtOriginXSet.Text, out originXSet);
            CWeldController.mDockMain.DrawAction();
        }

        private void txtOriginYSet_TextChanged(object sender, EventArgs e)
        {
            int.TryParse(txtOriginYSet.Text, out originYSet);
            CWeldController.mDockMain.DrawAction();
        }

        private void txtOriginZSet_TextChanged(object sender, EventArgs e)
        {
            int.TryParse(txtOriginZSet.Text, out originZSet);
            CWeldController.mDockMain.DrawAction();
        }
        private void txtPwmFrequencyReset_TextChanged(object sender, EventArgs e)
        {
            uint.TryParse(txtPwmFrequencyReset.Text, out pwmFrequencyReset);
        }

        private void txtPwmFrequencyDefault_TextChanged(object sender, EventArgs e)
        {
            uint.TryParse(txtPwmFrequencyDefault.Text, out pwmFrequencyDefault);
        }

        private void txtPwmFrequencyChabu_TextChanged(object sender, EventArgs e)
        {
            uint.TryParse(txtPwmFrequencyChabu.Text, out pwmFrequencyChabu);
        }

        private void txtPwmFrequencyGum_TextChanged(object sender, EventArgs e)
        {
            uint.TryParse(txtPwmFrequencyGum.Text, out pwmFrequencyGum);
        }

        private void txtPwmFrequencyLineMotor_TextChanged(object sender, EventArgs e)
        {
            uint.TryParse(txtPwmFrequencyLineMotor.Text, out pwmFrequencyLineMotor);
        }

        private void txtChabuDelay_TextChanged(object sender, EventArgs e)
        {
            uint.TryParse(txtChabuDelay.Text, out pwmChabuDelay);
        }

        private void txtGumDelay_TextChanged(object sender, EventArgs e)
        {
            uint.TryParse(txtGumDelay.Text, out gumDelay);
        }

        private void txtPolarityCoorX_TextChanged(object sender, EventArgs e)
        {
            int.TryParse(txtPolarityCoorX.Text, out polarityCoorX);
        }

        private void txtPolarityCoorY_TextChanged(object sender, EventArgs e)
        {
            int.TryParse(txtPolarityCoorY.Text, out polarityCoorY);
        }

        private void txtPolarityCoorZ_TextChanged(object sender, EventArgs e)
        {
            int.TryParse(txtPolarityCoorZ.Text, out polarityCoorZ);
        }

        // 将要显示在treeViewAction中的项添加到List中
        private List<CTreeViewAction> mListTreeViewAction = new List<CTreeViewAction>();
        private delegate void Selected();

        private class CTreeViewAction
        {
            public CTreeViewAction(string str, Selected sel)
            {
                name = str;
                selected = new Selected(sel);
            }
            public string name;         // treeView项的显示名称
            public Selected selected;   // treeView项被选中后的执行函数
        }

        private void ShowActionTreeView()
        {
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.移动到.ToString(), MoveToSelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.直线插补.ToString(), DrawLineSelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.圆弧插补.ToString(), DrawArcSelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.点焊开.ToString(), WeldOnSelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.点焊关.ToString(), WeldOffSelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.点胶开.ToString(), GumOnSelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.点胶关.ToString(), GumOffSelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.Z轴压下.ToString(), ZdownSelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.Z轴抬起.ToString(), ZupSelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.取电池.ToString(), GetBatterySelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.吸电池.ToString(), CatchaBatterySelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.放下电池.ToString(), DropBatterySelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.旋转电池.ToString(), PolaritySelected));
            mListTreeViewAction.Add(new CTreeViewAction(WELD_ACTION.延时.ToString(), DelaySelected));

            treeViewAction.Nodes.Clear();
            foreach (CTreeViewAction action in mListTreeViewAction)
            {
                TreeNode treeNode = new TreeNode(action.name);
                treeNode.Name = action.name;
                treeViewAction.Nodes.Add(treeNode);
            }
        }

        private void treeViewAction_AfterSelect(object sender, TreeViewEventArgs e)
        {
            foreach (CTreeViewAction action in mListTreeViewAction)
            {
                if (treeViewAction.Nodes.Find(action.name, false).First().IsSelected)
                {
                    action.selected();
                }
            }
        }

        private WELD_ACTION mCmdSelected;

        private void SetVisibleAll(bool isVisiable)
        {
            lblPoint1.Visible = isVisiable;
            lblPoint2.Visible = isVisiable;
            lblRadius.Visible = isVisiable;
            lblMode.Visible = isVisiable;
            txtPoint1X.Visible = isVisiable;
            txtPoint1Y.Visible = isVisiable;
            txtPoint1Z.Visible = isVisiable;
            txtPoint2X.Visible = isVisiable;
            txtPoint2Y.Visible = isVisiable;
            txtPoint2Z.Visible = isVisiable;
            txtRadius.Visible = isVisiable;
            comboBoxMode.Visible = isVisiable;
        }

        private void MoveToSelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = "移到坐标";
            lblPoint1.Visible = true;
            txtPoint1X.Visible = true;
            txtPoint1Y.Visible = true;
            txtPoint1Z.Visible = true;
            mCmdSelected = WELD_ACTION.移动到;
        }

        private void DrawLineSelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = "线段起点";
            lblPoint2.Text = "线段终点";
            lblPoint1.Visible = true;
            lblPoint2.Visible = true;
            txtPoint1X.Visible = true;
            txtPoint1Y.Visible = true;
            txtPoint1Z.Visible = true;
            txtPoint2X.Visible = true;
            txtPoint2Y.Visible = true;
            txtPoint2Z.Visible = true;
            mCmdSelected = WELD_ACTION.直线插补;
        }

        private void DrawArcSelected()
        {
            lblPoint1.Text = "圆心坐标";
            SetVisibleAll(true);
            lblPoint2.Visible = false;
            txtPoint2X.Visible = false;
            txtPoint2Y.Visible = false;
            txtPoint2Z.Visible = false;
            mCmdSelected = WELD_ACTION.圆弧插补;
        }

        private void WeldOnSelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = WELD_ACTION.点焊开.ToString();
            lblPoint1.Visible = true;
            mCmdSelected = WELD_ACTION.点焊开;
        }

        private void WeldOffSelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = WELD_ACTION.点焊关.ToString();
            lblPoint1.Visible = true;
            mCmdSelected = WELD_ACTION.点焊关;
        }

        private void GumOnSelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = WELD_ACTION.点胶开.ToString();
            lblPoint1.Visible = true;
            mCmdSelected = WELD_ACTION.点胶开;
        }

        private void GumOffSelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = WELD_ACTION.点胶关.ToString();
            lblPoint1.Visible = true;
            mCmdSelected = WELD_ACTION.点胶关;
        }

        private void ZdownSelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = WELD_ACTION.Z轴压下.ToString();
            lblPoint1.Visible = true;
            mCmdSelected = WELD_ACTION.Z轴压下;
        }

        private void ZupSelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = WELD_ACTION.Z轴抬起.ToString();
            lblPoint1.Visible = true;
            mCmdSelected = WELD_ACTION.Z轴抬起;
        }

        private void GetBatterySelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = WELD_ACTION.取电池.ToString();
            lblPoint1.Visible = true;
            mCmdSelected = WELD_ACTION.取电池;
        }

        private void CatchaBatterySelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = WELD_ACTION.吸电池.ToString();
            lblPoint1.Visible = true;
            mCmdSelected = WELD_ACTION.吸电池;
        }

        private void DropBatterySelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = WELD_ACTION.放下电池.ToString();
            lblPoint1.Visible = true;
            mCmdSelected = WELD_ACTION.放下电池;
        }

        private void PolaritySelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = "旋转步数";
            lblPoint1.Visible = true;
            txtPoint1X.Visible = true;
            mCmdSelected = WELD_ACTION.旋转电池;
        }

        private void DelaySelected()
        {
            SetVisibleAll(false);
            lblPoint1.Text = "延时(ms)";
            lblPoint1.Visible = true;
            txtPoint1X.Visible = true;
            mCmdSelected = WELD_ACTION.延时;
        }

        private ActionParas ParserActionParas()
        {
            ActionParas actionParar = new ActionParas();
            int point1X = 0;
            int point1Y = 0;
            int point1Z = 0;
            int point2X = 0;
            int point2Y = 0;
            int point2Z = 0;
            int radius = 0;
            int mode = 0;
            if (txtPoint1X.Visible)
            {
                int.TryParse(txtPoint1X.Text, out point1X);
            }
            if (txtPoint1Y.Visible)
            {
                int.TryParse(txtPoint1Y.Text, out point1Y);
            }
            if (txtPoint1Z.Visible)
            {
                int.TryParse(txtPoint1Z.Text, out point1Z);
            }
            if (txtPoint2X.Visible)
            {
                int.TryParse(txtPoint2X.Text, out point2X);
            }
            if (txtPoint2Y.Visible)
            {
                int.TryParse(txtPoint2Y.Text, out point2Y);
            }
            if (txtPoint2Z.Visible)
            {
                int.TryParse(txtPoint2Z.Text, out point2Z);
            }
            if (txtRadius.Visible)
            {
                int.TryParse(txtRadius.Text, out radius);
            }
            if (comboBoxMode.Visible)
            {
                mode = comboBoxMode.SelectedIndex + 1;
            }

            actionParar.Cmd = mCmdSelected;
            actionParar.Point1X = point1X;
            actionParar.Point1Y = point1Y;
            actionParar.Point1Z = point1Z;
            actionParar.Point2X = point2X;
            actionParar.Point2Y = point2Y;
            actionParar.Point2Z = point2Z;
            actionParar.Radius = radius;
            actionParar.Mode = mode;

            return actionParar;
        }

        private void btnInsertAction_Click(object sender, EventArgs e)
        {
            ActionParas actionParar = ParserActionParas();

            int index = 0;
            if (dgvAutoGumList.SelectedCells.Count != 0)
            {
                index = dgvAutoGumList.SelectedCells[0].RowIndex + 1;
            }
            else
            {
                index = dgvAutoGumList.Rows.Count;
            }
            mAutoActionList.Insert(index, actionParar);

            // 将当前操作的行设为选定状态，并滚动到
            dgvAutoGumList.ClearSelection();
            dgvAutoGumList.Rows[index].Selected = true;
            dgvAutoGumList.CurrentCell = dgvAutoGumList.SelectedCells[0];
        }

        private void btnReplaceAction_Click(object sender, EventArgs e)
        {
            ActionParas actionParar = ParserActionParas();
            int index = 0;
            if (dgvAutoGumList.SelectedCells.Count != 0)
            {
                index = dgvAutoGumList.SelectedCells[0].RowIndex;
                mAutoActionList[index] = actionParar;
            }
            else
            {
                if (dgvAutoGumList.Rows.Count == 0)
                {
                    mLog.LogMessage("没有可以替换的指令\n");
                }
                else
                {
                    mLog.LogMessage("请先选中要替换的行\n");
                }
                return;
            }

            // 将当前操作的行设为选定状态，并滚动到
            dgvAutoGumList.ClearSelection();
            dgvAutoGumList.Rows[index].Selected = true;
            dgvAutoGumList.CurrentCell = dgvAutoGumList.SelectedCells[0];
        }

        private void treeViewMenu_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeViewMenu.Nodes[0].IsSelected)
            {
                panel1.Show();
                panel2.Hide();
                panel3.Hide();
                panel4.Hide();
            }
            if (treeViewMenu.Nodes[1].IsSelected)
            {
                panel1.Hide();
                panel2.Show();
                panel3.Hide();
                panel4.Hide();
            }
            if (treeViewMenu.Nodes[2].IsSelected)
            {
                panel1.Hide();
                panel2.Hide();
                panel3.Show();
                panel4.Hide();
            }
            if (treeViewMenu.Nodes[3].IsSelected)
            {
                panel1.Hide();
                panel2.Hide();
                panel3.Hide();
                panel4.Show();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        
        private void btnSetPara_Click(object sender, EventArgs e)
        {
            if (SendAllPararmeter())
            {
                this.Hide();
            }
        }
    }
}
